﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomerService
{
    public partial class tabRefundsReturns : System.Web.UI.UserControl
    {
        private SalesManagerEntities _context;
        protected tblOrderHeader _orderHeader = _emptyOrder;
        private static tblOrderHeader _emptyOrder = new tblOrderHeader();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public new void Load(SalesManagerEntities context, tblOrderHeader orderHeader, bool forceUpdate)
        {
            _context = context;
            _orderHeader = orderHeader;
            txtOrderID.Text = _orderHeader.OrderID.ToString();

            if (!IsPostBack || forceUpdate || (Request.Params["__EVENTARGUMENT"] == "UpdateDetails"))
            {
                ListViewReturns.DataSource = _orderHeader.RMAReturns.ToArray();
                ListViewReturns.DataBind();

                ListViewRefunds.DataSource = _orderHeader.Refunds;
                ListViewRefunds.DataBind();
            }
        }

        protected void btnRemove_Click(object sender, CommandEventArgs e)
        {
            int key = int.Parse(e.CommandArgument.ToString());

            tblRefund refund = _context.tblRefund.Find(new object[] { key });

            // If there is a pending transaction for this refund then cancel it.
            if (refund.Transaction != null && refund.Transaction.TransactionStatusCode == "E")
                refund.Transaction.TransactionStatusCode = "X";

            _context.tblRefund.Remove(refund);
            ListViewRefunds.DataSource = _orderHeader.Refunds.ToArray();
            ListViewRefunds.DataBind();
        }

    }
}